package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult deleteSensitiveById(Integer id) {
        //检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行操作
        boolean flag = removeById(id);
        if (flag){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult selectSensitiveList(SensitiveDto sensitiveDto) {
        //检查参数
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查page
        sensitiveDto.checkParam();
        //开启分页查询
        IPage<AdSensitive> page = new Page<AdSensitive>(sensitiveDto.getPage(),sensitiveDto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        //name判断,值有效则模糊查询name
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        // 添加时间排序
        queryWrapper.orderByAsc(AdSensitive::getCreatedTime);
        page = page(page,queryWrapper);
        //封装result
        return new PageResponseResult(sensitiveDto.getPage(),
                sensitiveDto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //检查参数
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives()) || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行，根据布尔值返回result
        boolean flag = updateById(adSensitive);
        if (flag){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //检查参数
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //添加创建时间
        adSensitive.setCreatedTime(new Date());
        //执行添加
        boolean flag = save(adSensitive);
        if (flag){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }

    @Override
    public List<String> findAllSensitive() {
        return this.baseMapper.findAllSensitive();
    }
}
